package alg;
import java.text.DecimalFormat;
import java.util.Random;

public class PMC {

	double[][] w1;
	double[] w2;
	
	
	public PMC(int n_neuronios1,int n_entradas1){
		
		w1 = new double[n_neuronios1][n_entradas1];
		w2 = new double[n_neuronios1+1];
		
		
	}
	
	public void inicializar() {

		
		Random Gerador = new Random();

		for (int j = 0; j < w1.length; j++) {
			for (int i = 0; i < w1[j].length; i++) {

				w1[j][i] = Gerador.nextFloat();
			}
		}

		System.out.print("\n");
		for (int i = 0; i < w2.length; i++) {

			w2[i] = Gerador.nextFloat();
		}

	}

	public int treinar(double[][] matriz, double[] d, int nSamples,double e) {

		double n = 0.1;
		int epoca = 0;
		double[] y1 = new double[w2.length];
		double[] l1 = new double[w1.length];
		double[] y2 = new double[matriz.length];
		double l2;
		double[] fi1 = new double[w1.length];
		double fi2;

		double atual = 0;

		for (int i = 0; i < nSamples; i++) {

			y2[i] = 0;

		}

		do {

			for (int a = 0; a < nSamples; a++) {

				for (int i = 0; i < w2.length; i++) {
					y1[i] = 0;

				}

				l2 = 0;
				fi2 = 0;

				for (int i = 0; i < w1.length; i++) {

					l1[i] = 0;
					fi1[i] = 0;

				}

				for (int i = 0; i < w2.length; i++) {

					if (i == 0) {

						y1[i] = -1;

					}

					else {

						for (int j = 0; j < w1[0].length; j++) {

							l1[i - 1] = l1[i - 1]
									+ (w1[i - 1][j] * matriz[a][j]);

						}

						y1[i] = 1 / (1 + Math.exp(-1 * l1[i - 1]));

					}

				}

				for (int i = 0; i < w2.length; i++) {

					l2 = l2 + (w2[i] * y1[i]);

				}

				y2[a] = 1 / (1 + Math.exp(-1 * l2));

				fi2 = (d[a] - y2[a]) * (y2[a] * (1 - y2[a]));

				for (int i = 0; i < w2.length; i++) {

					w2[i] = w2[i] + (n * fi2 * y1[i]);

				}

				for (int i = 0; i < w1.length; i++) {

					fi1[i] = (fi2 * w2[i + 1]) * (y1[i + 1] * (1 - y1[i + 1]));

				}

				for (int j = 0; j < w1.length; j++) {
					for (int i = 0; i < w1[0].length; i++) {

						w1[j][i] = w1[j][i] + (n * fi1[j] * matriz[a][i]);

					}
				}

				for (int i = 0; i < w2.length; i++) {
					y1[i] = 0;

				}

				l2 = 0;
				fi2 = 0;

				for (int i = 0; i < w1.length; i++) {

					l1[i] = 0;
					fi1[i] = 0;

				}

				for (int i = 0; i < w2.length; i++) {

					if (i == 0) {

						y1[i] = -1;

					}

					else {

						for (int j = 0; j < w1[0].length; j++) {

							l1[i - 1] = l1[i - 1]
									+ (w1[i - 1][j] * matriz[a][j]);

						}

						y1[i] = 1 / (1 + Math.exp(-1 * l1[i - 1]));

					}

				}

				for (int i = 0; i < w2.length; i++) {

					l2 = l2 + (w2[i] * y1[i]);

				}

				y2[a] = 1 / (1 + Math.exp(-1 * l2));

			}

			atual = eqm(y2,d,nSamples);
			
			epoca = epoca + 1;

		} while (atual >= e);

return epoca;
		
	}

	public double eqm(double y2[], double d[],int nSamples) {

		double e = 0;

		for (int i = 0; i < nSamples; i++) {

			e = e + (Math.pow((d[i] - y2[i]), 2) / 2);

		}

		e = (e / nSamples);

		return e;

	}

	public String classificar(double[] value) {

		double R = 0;
		double[] y1 = new double[w2.length];
		double[] l1 = new double[w1.length];
		double l2 = 0;

		for (int i = 0; i < w2.length; i++) {

			if (i == 0) {

				y1[i] = -1;

			}

			else {

				for (int j = 0; j < w1[0].length; j++) {

					l1[i - 1] = l1[i - 1] + (w1[i - 1][j] * value[j]);

				}

				y1[i] = 1 / (1 + Math.exp(-1 * l1[i - 1]));

			}

		}

		for (int i = 0; i < w2.length; i++) {

			l2 = l2 + (w2[i] * y1[i]);

		}

		R = 1 / (1 + Math.exp(-1 * l2));
		
		DecimalFormat df = new DecimalFormat("0.##");
		String saida = df.format(R*100);

		return saida;

	}

}
